// SlagCar/OrderList/OrderList.js
let listBehavior = require('../../behavior/list')
Page({
  behaviors: [ listBehavior, ],

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    query: {},
    page: 1,
    total: 0,
    limit: 10,
    searchValue: '' //搜索内容
  },

  //获取数据
  getDataList(){
    console.log(111)
    let {page,limit,filter,list,query} = this.data;
    let total = 0,
      obj = {};
    let url = 'DynamicQuery/query_dynamic_v3',
      post = {"oby":"muck_order_main.is_used,muck_order_main.end_time","table_id":"muck_order_main",page,limit};
    if (query.length) post.SearchWhere = {filters: query,type: 'and'};
    wx.$post(3,url,post).then(res => {
      if (res.errcode != 0) return;

      if (res.data.row){
        total = res.data.total;
        let rows = res.data.row;
        if(page !== 1) rows = [ ...list,...rows, ]
        obj = {total,list: rows,page};
      }
      this.setData(obj);
    })
  },

  searchChange(e){
    this.setData({searchValue: e.detail});
  },
  // 搜索
  bindSearch(e){
    let value = e.detail,
      {deliveryList,searchValue} = this.data,
      query = [];
    if (searchValue !== ''){
      if (isNaN(Number(searchValue))){
        deliveryList.forEach(item => {
          if (item.delivery_name && item.delivery_name.indexOf(searchValue)>=0) query.push({CompareWith: "muck_order_main.reference1_all_delivery", Operator: "=", CompareValue: item.row_id, or: 1})
        });
        if (!query.length){
          query.push({CompareWith: "muck_order_main.reference1_all_delivery", Operator: "=", CompareValue: 0, or: 1})
        }
      }else {
        query.push({CompareWith: "muck_order_main.construction_price", Operator: "=", CompareValue: searchValue, or: 0})
      }
    }
    this.data.query = query;
    this.getFirstPage();
  },
  //获取工地
  getDelivery(){
    return new Promise(resolve => {
      this.getCustomData('all_delivery').then(res => {
        this.data.deliveryList =  res || [];
        resolve();
      })
    })
  },
  //获取diy数据
  getCustomData(table_id){
    console.log(222)
    let url = 'DynamicQuery/query_dynamic_v3';
    return new Promise(resolve => {
      wx.$post(3,url,{limit: 0, page: 1, table_id,obj: `${table_id}.row_id`}).then(res => {
        if (res.errcode != 0) return;
        resolve(res.data.row)
      })
    })
  },

  navTo(e){
    let {url} = e.currentTarget.dataset;
    wx.navigateTo({
      url
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    this.getDataList();
    this.getDelivery();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
